/**
 	LoaderHandler Class
 */
package nrb.utils
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.net.*;
	import flash.utils.Timer;
	
	import mx.containers.*;
	import mx.controls.*;
	import mx.core.Application;
	import mx.managers.PopUpManager;
	
	import nrb.utils.gif.player.GIFPlayer;
	
	/**
	 	Type: Class
	 	@Params:none
	 	@return:none
	 	Desc: Class for loader animation
	 */
	public class LoaderHandler extends Sprite {
		
		/**
		 	variables for progress bar, timer, application
		 */
		//[Embed(source='embed/loading.gif')] private static const loader_image:Class;
		private var progress_bar:ProgressBar;
		private var timer:Timer;
		private var ap:Application;
		private var image:Image = new Image();
		private var label:Label = new Label();
		private var hbox:HBox = new HBox();
		private var gif_player:GIFPlayer = new GIFPlayer();
		private var loader_type:String = 'CIRCLE'
		
		/**
		 	Type: Controller
		 	@Params:none
		 	@return:none
		 	Desc: Initializes variables and configure listener
		 */
		public function LoaderHandler() {
			if(loader_type == 'CIRCLE')
				set_circle_loader();
			else
				set_bar_loader();
		}
		
		/**
		 	Type: Public Function
		 	@Params:none
		 	@return:void
		 	Desc: shows the circle loader in center screen
		 */
		private function set_circle_loader(): void {
			ap = new Application();
			gif_player.load(new URLRequest('loading.gif'));
			image.source = gif_player;
			label.text = 'Loading...';
			hbox.addChild(image);
			hbox.addChild(label);
			ap.addChild(hbox);
			ap.width = 300;
			ap.height = 80;
		}
		
		/**
		 	Type: Public Function
		 	@Params:none
		 	@return:void
		 	Desc: shows the bar loader in center screen
		 */
		private function set_bar_loader(): void {
			ap = new Application();
			progress_bar = new ProgressBar();
			progress_bar.mode = 'manual';
			progress_bar.labelPlacement = 'center';
			progress_bar.label = "Loading...";
			timer = new Timer(30);
			timer.addEventListener(TimerEvent.TIMER, listener_timer);
			ap.addChild(progress_bar);
		}
		
		/**
		 	Type: Public Function
		 	@Params:parent[DisplayObject] AS DisplayObject
		 	@return:void
		 	Desc: shows the loader in center screen
		 */
		public function show_loader(parent:DisplayObject): void {
			PopUpManager.addPopUp(ap, parent, true);
			PopUpManager.centerPopUp(ap);
			ap.setFocus();
			if(loader_type == 'BAR')
				play_progress_bar();
		}
		
		/**
		 	Type: Public Function
		 	@Params:none
		 	@return:void
		 	Desc: removes the loader in center screen
		 */
		public function remove_loader(): void {
			PopUpManager.removePopUp(ap);
		}
		
		/**
		 	Type: Private Function
		 	@Params:none
		 	@return:void
		 	Desc: listener for timer, while timer increments progress bar increases by 1 limit 100
		 */
		 
		private function listener_timer(event:TimerEvent): void {
			progress_bar.setProgress(progress_bar.value + 1, 100);
		}
		
		
		/**
		 	Type: Private Function
		 	@Params:none
		 	@return:void
		 	Desc: resets progress bar to 0 limit 100
		 */
		 
		private function reset_progress_bar(): void {
			progress_bar.setProgress(0, 100);
			progress_bar.scaleX = 1.0;
			progress_bar.scaleY = 1.0;
			progress_bar.alpha = 1.0;
		}
		
		
		/**
		 	Type: Private Function
		 	@Params:none
		 	@return:void
		 	Desc: starts loading of progress bar
		 */
		 
		private function play_progress_bar(): void {
			reset_progress_bar();
			timer.start();
		}
		
		
		
		
		
		
	}
}